package com.zcxx.cotton.sale.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.zcxx.cotton.sale.domain.BuySubsidyRecords;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CottonSubsidyConverter {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        // 配置ObjectMapper忽略未知字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    }

    /**
     * 将JSON字符串转换为BuySubsidyRecords对象
     */
    public static BuySubsidyRecords convertFromJson(String jsonString) {
        try {
            JsonNode rootNode = objectMapper.readTree(jsonString);
            BuySubsidyRecords record = new BuySubsidyRecords();

            // 设置基础字段
            if (rootNode.has("_id")) {
                record.setId(rootNode.get("_id").asText());
            }
            if (rootNode.has("slist_num")) {
                record.setSlistNum(rootNode.get("slist_num").asText());
            }
            if (rootNode.has("factory_name")) {
                record.setFactoryName(rootNode.get("factory_name").asText());
            }

            // 处理speasant对象
            if (rootNode.has("speasant")) {
                JsonNode speasantNode = rootNode.get("speasant");

                if (speasantNode.has("subsidy_num")) {
                    record.setSubsidyNum(speasantNode.get("subsidy_num").asText());
                }
                if (speasantNode.has("partyname")) {
                    record.setPartyname(speasantNode.get("partyname").asText());
                }
                if (speasantNode.has("certnumber")) {
                    record.setCertnumber(speasantNode.get("certnumber").asText());
                }
                if (speasantNode.has("address")) {
                    record.setAddress(speasantNode.get("address").isNull() ? null : speasantNode.get("address").asText());
                }
                if (speasantNode.has("planter_flag")) {
                    record.setPlanterFlag(speasantNode.get("planter_flag").asText());
                }
                if (speasantNode.has("speasant_name")) {
                    record.setSpeasantName(speasantNode.get("speasant_name").asText());
                }
                if (speasantNode.has("speasant_phone")) {
                    record.setSpeasantPhone(speasantNode.get("speasant_phone").asText());
                }
                if (speasantNode.has("speasant_plant")) {
                    record.setSpeasantPlant(speasantNode.get("speasant_plant").asText());
                }
                if (speasantNode.has("speasant_pic")) {
                    record.setSpeasantPic(speasantNode.get("speasant_pic").asText());
                }

                // 处理region对象
                if (speasantNode.has("region")) {
                    JsonNode regionNode = speasantNode.get("region");
                    BuySubsidyRecords.RegionInfo regionInfo = objectMapper.treeToValue(regionNode, BuySubsidyRecords.RegionInfo.class);

                    // 将region信息映射到主对象
                    record.setFarmerPlantAddress(regionInfo.getFarmerPlantAddress());
                    record.setRegionName(regionInfo.getRegionName());
                    record.setCropArea(regionInfo.getCropArea());
                    record.setBuyVariety(regionInfo.getCottonVariety());
                    record.setTraceNo(regionInfo.getTraceNo());
                    record.setRatifyLandType(regionInfo.getRatifyLandType());
                    record.setForecastYield(regionInfo.getForecastYield());
                    record.setCompanyName(regionInfo.getCompanyName());
                    record.setUsci(regionInfo.getUsci());
                    record.setSeedSale(regionInfo.getSeedSale());
                }
            }

            // 处理scale对象
            if (rootNode.has("scale")) {
                JsonNode scaleNode = rootNode.get("scale");

                if (scaleNode.has("nqy_weight")) {
                    record.setNqyWeight(scaleNode.get("nqy_weight").asText());
                }
                if (scaleNode.has("gross_weight")) {
                    record.setGrossWeight(scaleNode.get("gross_weight").asText());
                }
                if (scaleNode.has("tare_weight")) {
                    record.setTareWeight(scaleNode.get("tare_weight").asText());
                }
                if (scaleNode.has("gross_way")) {
                    record.setGrossWay(scaleNode.get("gross_way").asLong());
                }
                if (scaleNode.has("tare_way")) {
                    record.setTareWay(scaleNode.get("tare_way").asLong());
                }
                if (scaleNode.has("gross_date")) {
                    String grossDateStr = scaleNode.get("gross_date").asText();
                    record.setGrossDate(grossDateStr);
                }
                if (scaleNode.has("tare_date")) {
                    String tareDateStr = scaleNode.get("tare_date").asText();
                    record.setTareDate(tareDateStr);
                }
                if (scaleNode.has("buttress_weight")) {
                    record.setButtressWeight(scaleNode.get("buttress_weight").asText());
                }
                if (scaleNode.has("nmembrane_skin")) {
                    record.setNmembraneSkin(scaleNode.get("nmembrane_skin").asText());
                }
            }

            // 处理agr对象
            if (rootNode.has("agr")) {
                JsonNode agrNode = rootNode.get("agr");

                if (agrNode.has("agr_company_id")) {
                    record.setAgrCompanyId(agrNode.get("agr_company_id").asText());
                }
                if (agrNode.has("agr_company_name")) {
                    record.setAgrCompanyName(agrNode.get("agr_company_name").asText());
                }
                if (agrNode.has("agr_section_id")) {
                    record.setAgrSectionId(agrNode.get("agr_section_id").asText());
                }
                if (agrNode.has("agr_section_name")) {
                    record.setAgrSectionName(agrNode.get("agr_section_name").asText());
                }
                if (agrNode.has("agr_nwet")) {
                    record.setAgrNwet(agrNode.get("agr_nwet").asText());
                }
                if (agrNode.has("agr_nimpurity")) {
                    record.setAgrNimpurity(agrNode.get("agr_nimpurity").asText());
                }
            }

            // 处理scale_pic对象
            if (rootNode.has("scale_pic")) {
                JsonNode scalePicNode = rootNode.get("scale_pic");
                BuySubsidyRecords.ScalePictures scalePictures = objectMapper.treeToValue(scalePicNode, BuySubsidyRecords.ScalePictures.class);

                record.setGrossAfterPic(scalePictures.getGrossAfterName());
                record.setGrossLaterPic(scalePictures.getGrossLaterName());
                record.setTareAfterPic(scalePictures.getTareAfterName());
                record.setTareLaterPic(scalePictures.getTareLaterName());
            }
            Date currentDate = new Date();
            // 创建格式化器
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            // 格式化日期
            String formattedDate = formatter.format(currentDate);
            // 设置系统时间
            record.setCreateTime(formattedDate);
            record.setUpdateTime(formattedDate);

            return record;

        } catch (Exception e) {
            throw new RuntimeException("JSON转换失败: " + e.getMessage(), e);
        }
    }

    /**
     * 简化版转换方法 - 使用Jackson的自动映射
     */
    public static BuySubsidyRecords convertSimple(String jsonString) {
        try {
            // 使用自定义反序列化器或直接映射
            return objectMapper.readValue(jsonString, BuySubsidyRecords.class);
        } catch (Exception e) {
            throw new RuntimeException("JSON转换失败: " + e.getMessage(), e);
        }
    }
}
